local jieyin = fk.CreateSkill {

  name = "joy_mou__jieyin",

  tags = {  },

}



jieyin:addEffect("active", {
  name = "joy_mou__jieyin",
  anim_type = "support",
  prompt = "结姻：选择一名有“姻”的其他角色。",
  interaction = function(self,player)
    return UI.ComboBox {choices = {"交给一张牌","置入装备牌"}}
  end,
  card_num = 1,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(jieyin.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, player, to_select, selected)
    local ch = self.interaction.data
    if ch == "置入装备牌" then
      return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip
    else
      return #selected == 0 and Fk:currentRoom():getCardArea(to_select) == Player.Hand
    end
  end,
  target_filter = function(self, player, to_select, selected)
    if #selected == 0 and to_select ~= player.id and #selected_cards == 1 then
      local target = Fk:currentRoom():getPlayerById(to_select)
      if target and target:getMark("@@joy_mou_yan") > 0 then
        local ch = self.interaction.data
        if ch == "置入装备牌" then
          local card = Fk:getCardById(selected_cards[1])
          return #target:getAvailableEquipSlots(card.sub_type) > 0
        else
          return true
        end
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local ch = self.interaction.data
    if ch == "置入装备牌" then
      room:moveCards({
        ids = effect.cards,
        from = effect.from,
        to = effect.tos[1],
        toArea = Card.PlayerEquip,
        skillName = jieyin.name,
        moveReason = fk.ReasonPut,
      })
    else
      room:obtainCard(target, effect.cards[1], false, fk.ReasonGive)
    end
    if player.dead then return end
    if player:isWounded() then
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = jieyin.name
      })
    end
    if not player.dead then
      player:drawCards(1,jieyin.name)
    end
  end,
})

jieyin:addEffect(fk.GameStart, {
  name = "#joy_mou__jieyin_trigger",
  events = {fk.GameStart, fk.Death},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill(jieyin.name) and #player.room.alive_players > 0
    elseif event == fk.Death then
      return player:hasSkill(jieyin.name) and target:getMark("@@joy_mou_yan") > 0 and #target:getCardIds("hej") > 0
    end
  end,
  on_cost =  function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local targets = {}
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:getMark("@@joy_mou_yan") == 0 then
          table.insertIfNeed(targets,p.id)
        end
      end
      if #targets <= 0 then return false end
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "结姻：你可以选择一名其他角色，令其与你各获得“姻”标记，然后其获得技能〖结姻〗。", jieyin.name,true)
      if #tos > 0 then
        event:setCostData(self, tos[1])
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      room:notifySkillInvoked(player, jieyin.name,"special")
      player:broadcastSkillInvoke(jieyin.name, 1)
      local to = room:getPlayerById(event:getCostData(self))
      room:setPlayerMark(to, "@@joy_mou_yan", 1)
      room:setPlayerMark(player, "@@joy_mou_yan", 1)
      room:handleAddLoseSkills(to,"joy_mou__jieyin",nil,true)
      room:changeKingdom(player,to.kingdom, true)
    elseif event == fk.Death then
      room:notifySkillInvoked(player, jieyin.name,"drawcard")
      local cards = target:getCardIds("hej")
      if #cards > 0 then
        local dummy = Fk:cloneCard'slash'
        dummy:addSubcards(cards)
        room:obtainCard(player, dummy, false, fk.ReasonPrey)
      end
    end
  end,
})
jieyin:addEffect(fk.Death, {
  name = "#joy_mou__jieyin_trigger",
  events = {fk.GameStart, fk.Death},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill(jieyin.name) and #player.room.alive_players > 0
    elseif event == fk.Death then
      return player:hasSkill(jieyin.name) and target:getMark("@@joy_mou_yan") > 0 and #target:getCardIds("hej") > 0
    end
  end,
  on_cost =  function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local targets = {}
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:getMark("@@joy_mou_yan") == 0 then
          table.insertIfNeed(targets,p.id)
        end
      end
      if #targets <= 0 then return false end
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "结姻：你可以选择一名其他角色，令其与你各获得“姻”标记，然后其获得技能〖结姻〗。", jieyin.name,true)
      if #tos > 0 then
        event:setCostData(self, tos[1])
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      room:notifySkillInvoked(player, jieyin.name,"special")
      player:broadcastSkillInvoke(jieyin.name, 1)
      local to = room:getPlayerById(event:getCostData(self))
      room:setPlayerMark(to, "@@joy_mou_yan", 1)
      room:setPlayerMark(player, "@@joy_mou_yan", 1)
      room:handleAddLoseSkills(to,"joy_mou__jieyin",nil,true)
      room:changeKingdom(player,to.kingdom, true)
    elseif event == fk.Death then
      room:notifySkillInvoked(player, jieyin.name,"drawcard")
      local cards = target:getCardIds("hej")
      if #cards > 0 then
        local dummy = Fk:cloneCard'slash'
        dummy:addSubcards(cards)
        room:obtainCard(player, dummy, false, fk.ReasonPrey)
      end
    end
  end,
})

return jieyin